package com.danjinwulian.device.service.impl;

import com.danjinwulian.common.constant.IoTConstants;
import com.danjinwulian.common.constant.UserConstants;
import com.danjinwulian.common.core.domain.entity.IotDevice;
import com.danjinwulian.common.utils.DateUtils;
import com.danjinwulian.device.mapper.IotDeviceMapper;
import com.danjinwulian.device.service.IIotDeviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备 业务层处理
 * 
 * @author ken.h
 */
@Service
public class IIotDeviceServiceImpl implements IIotDeviceService
{
    private static final Logger log = LoggerFactory.getLogger(IIotDeviceServiceImpl.class);

    @Autowired
    private IotDeviceMapper deviceMapper;

    @Override
    public List<IotDevice> selectDeviceList(IotDevice iotDevice) {
        return deviceMapper.selectDeviceList(iotDevice);
    }

    @Override
    public int selectDeviceCount() {
        return deviceMapper.selectDeviceCount();
    }

    @Override
    public IotDevice selectDeviceByRoomName(String roomName) {
        return deviceMapper.selectDeviceByRoomName(roomName);
    }

    @Override
    public IotDevice selectDeviceById(Long deviceId) {
        return deviceMapper.selectDeviceById(deviceId);
    }

    @Override
    public IotDevice selectDeviceByDeviceName(String deviceName) {
        return deviceMapper.selectDeviceByDeviceName(deviceName);
    }

    @Override
    public int insertDevice(IotDevice iotDevice) {
        return deviceMapper.insertDevice(iotDevice);
    }

    @Override
    public int updateDevice(IotDevice iotDevice) {
        return deviceMapper.updateDevice(iotDevice);
    }

    @Override
    public int deleteDeviceById(Long deviceId) {
        return deviceMapper.deleteDeviceById(deviceId);
    }

    @Override
    public int deleteDeviceByIds(Long[] deviceIds) {
        return deviceMapper.deleteDeviceByIds(deviceIds);
    }

    @Override
    public String importDevice(List<IotDevice> deviceList, Boolean isUpdateSupport, String operName) {
        return null;
    }

    @Override
    public String checkDeviceNameUnique(String deviceName) {
        int count = deviceMapper.checkDeviceNameUnique(deviceName);
        if (count > 0)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkRoomNameUnique(IotDevice iotDevice) {
        int count = deviceMapper.checkRoomNameUnique(iotDevice);
        if (count > 0)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int updateDeviceStatus(IotDevice iotDevice) {
        return deviceMapper.updateDevice(iotDevice);
    }

    @Override
    public int updateCircuitSwitch(IotDevice iotDevice) {
        if (IoTConstants.DEVICE_SCAN_POWERON.equals(iotDevice.getCircuitSwitch()) || IoTConstants.DEVICE_REMOTE_POWERON.equals(iotDevice.getCircuitSwitch()) || IoTConstants.DEVICE_AUTO_POWERON.equals(iotDevice.getCircuitSwitch()))
        {
            iotDevice.setLastPoweronTime(DateUtils.getNowDate());
        }

        return deviceMapper.updateDevice(iotDevice);
    }

    @Override
    public int updateScanPoweron(IotDevice iotDevice) {
        return deviceMapper.updateDevice(iotDevice);
    }

    @Override
    public int updateLavePoweroff(IotDevice iotDevice) {
        return deviceMapper.updateDevice(iotDevice);
    }

    @Override
    public List<IotDevice> selectOfflineExceptionDeviceList() {
        return deviceMapper.selectOfflineExceptionDeviceList();
    }

    @Override
    public List<IotDevice> selectPoweroffExceptionDeviceList() {
        return deviceMapper.selectPoweroffExceptionDeviceList();
    }
}
